// 收藏工具类
const collectionUtil = {
  // 基础API地址
  baseUrl: 'http://localhost:8081/user',
  
  // 添加收藏
  addCollection(userId, entityType, entityId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${this.baseUrl}/CollAdd`,
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          userId: userId,
          entityType: entityType,
          entityId: entityId,
          collectTime: new Date().toISOString()
        },
        success: (res) => {
          if (res.data && res.data.includes('成功')) {
            resolve({ success: true, message: res.data })
          } else {
            resolve({ success: false, message: res.data || '收藏失败' })
          }
        },
        fail: (err) => {
          reject({ success: false, message: '网络错误，请重试', error: err })
        }
      })
    })
  },

  // 取消收藏
  removeCollection(userId, entityType, entityId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${this.baseUrl}/CollRemove`,
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          userId: userId,
          entityType: entityType,
          entityId: entityId
        },
        success: (res) => {
          if (res.data && res.data.includes('成功')) {
            resolve({ success: true, message: res.data })
          } else {
            resolve({ success: false, message: res.data || '取消收藏失败' })
          }
        },
        fail: (err) => {
          reject({ success: false, message: '网络错误，请重试', error: err })
        }
      })
    })
  },

  // 获取用户收藏列表
  getCollections(userId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${this.baseUrl}/CollAll?userid=${userId}`,
        method: 'GET',
        success: (res) => {
          if (res.data && Array.isArray(res.data)) {
            resolve({ success: true, data: res.data })
          } else {
            resolve({ success: false, data: [], message: '获取收藏列表失败' })
          }
        },
        fail: (err) => {
          reject({ success: false, data: [], message: '网络错误，请重试', error: err })
        }
      })
    })
  },

  // 检查是否已收藏
  async checkCollectionStatus(userId, entityType, entityId) {
    try {
      const result = await this.getCollections(userId)
      if (result.success) {
        return result.data.some(item => 
          item.entityType === entityType && item.entityId == entityId
        )
      }
      return false
    } catch (error) {
      console.error('检查收藏状态失败:', error)
      return false
    }
  },

  // 批量删除收藏
  batchRemoveCollections(collections) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${this.baseUrl}/CollBatchRemove`,
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        data: collections,
        success: (res) => {
          if (res.data && res.data.includes('成功')) {
            resolve({ success: true, message: res.data })
          } else {
            resolve({ success: false, message: res.data || '批量删除失败' })
          }
        },
        fail: (err) => {
          reject({ success: false, message: '网络错误，请重试', error: err })
        }
      })
    })
  },

  // 显示操作结果
  showResult(result, successTitle = '操作成功', failTitle = '操作失败') {
    if (result.success) {
      wx.showToast({
        title: successTitle,
        icon: 'success'
      })
    } else {
      wx.showToast({
        title: result.message || failTitle,
        icon: 'none'
      })
    }
  }
}

module.exports = collectionUtil
